(function() {
	/* function getPro(file) {
		return new Promise(function(resolve, reject) {
			$.ajax({
				url: 'data/' + file,
				dataType: 'json',
				success(res) {
					resolve(res);
				}
			});
		});
	} */
	/* async function getData(file) {
		// let categoryList = await getPro(file);
		// console.log(categoryList);
		let countryList = await getPro(file);
		console.log(countryList);
	} */
	//------------分类----------------------
	// let div =
	// 	`<div class="text-sm hand py-1 px-2 rounded my-1 mx-2 text-gray text-hover-red">${val.category_name}</div>`
	// getData('category_list.json');
	// getData('country_list.json');
	// console.log(categoryList);

	//------------分类----------------------
	async function getCategoryList() {
		let {
			data: r
		} = await axios({
			url: 'data/category_list.json',
		});

		let div = ``;
		r.forEach(function(val) {
			div +=
				`<div class="text-sm hand py-1 px-2 rounded my-1 mx-2 text-gray text-hover-red category-click">${val.category_name}</div>`;
		});
		$('.category').append(div);
		$('.category').append($(div));

		//绑定事件
		$('.category-click').click(async function() {
			let categoryName = $(this).html();

			let {
				data: r
			} = await axios({
				url: 'data/film_list.json',
			});

			r = r.filter(function(val) {
				return val.category_list.find(function(c) {
					return c.category_name == categoryName
				})
			});
			//重写渲染电影列表
			getFilmList(r);
		})
	}
	getCategoryList();

	//------------区域----------------------
	async function getCountryList() {
		let {
			data: r
		} = await axios({
			url: 'data/country_list.json'
		});
		let div = ``;
		r.forEach(function(val) {
			div +=
				`<div class="text-sm hand py-1 px-2 rounded my-1 mx-2 text-gray text-hover-red country-click">${val.country_name}</div>`;
		});
		$('.country').append(div);

		//绑定事件
		$('.country-click').click(async function() {
			let countryName = $(this).html();

			let {
				data: r
			} = await axios({
				url: 'data/film_list.json',
			});

			r = r.filter(function(val) {
				return val.country_list.find(function(c) {
					return c.country_name == countryName
				})
			});
			//重写渲染电影列表
			getFilmList(r);
		})
	}
	getCountryList();

	//------------年代----------------------
	const date = new Date();
	let year = date.getFullYear();
	let div = ``;
	for (var y = year; y >= 2002; y--) {
		div +=
			`<div class="text-sm hand py-1 px-2 rounded my-1 mx-2 text-gray text-hover-red year-click">${y}年</div>`;
	}
	$('.yer').append(div);

	//绑定事件
	$('.year-click').click(async function() {
		let year = $(this).html();
		year = year.substring(0, year.length - 1);

		let {
			data: r
		} = await axios({
			url: 'data/film_list.json'
		})

		r = r.filter(function(val) {
			return val.film_release_date.includes(year)
		})
		getFilmList(r);
	})


	//------------影片----------------------
	async function getFilmList(r) {
		let a = ``;
		if (!r) {
			let {
				data: r
			} = await axios({
				url: 'data/film_list.json'
			});
			r.forEach(function(val) {
				a += getFilm(val)
			})
		} else {
			r.forEach(function(val) {
				a += getFilm(val)
			})
		}
		$('.film-list').empty();
		$('.film-list').append(a);

		//绑定事件(每一部影片)
		$('.film-click').click(function(e) {
			e.preventDefault()
			let filmId = $(this).children('input').val();
			//存储filmId
			localStorage.setItem("filmId", filmId);
			//跳转
			location.href = '影片详情.html';
		})
	}
	getFilmList();
})()
